package net.lab1024.sa.admin.module.business.oa.reservation.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import jakarta.validation.Valid;
import net.lab1024.sa.admin.module.business.oa.enterprise.domain.vo.EnterpriseVO;
import net.lab1024.sa.admin.module.business.oa.enterprise.service.EnterpriseService;
import net.lab1024.sa.admin.module.business.oa.reservation.constant.ReservationTypeEnum;
import net.lab1024.sa.admin.module.business.oa.reservation.dao.ReservationDao;
import net.lab1024.sa.admin.module.business.oa.reservation.domain.entity.ReservationEntity;
import net.lab1024.sa.admin.module.business.oa.reservation.domain.form.ReservationAddForm;
import net.lab1024.sa.admin.module.business.oa.reservation.domain.form.ReservationQueryForm;
import net.lab1024.sa.admin.module.business.oa.reservation.domain.form.ReservationQueryFormPub;
import net.lab1024.sa.admin.module.business.oa.reservation.domain.form.ReservationUpdateForm;
import net.lab1024.sa.admin.module.business.oa.reservation.domain.vo.ReservationVO;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;

/**
 * 预约记录 Service
 *
 * @Author TianYuan
 * @Date 2025-07-22 16:21:55
 * @Copyright TianYuan
 */

@Service
public class ReservationService {

    @Resource
    private ReservationDao reservationDao;

    @Resource
    private EnterpriseService enterpriseService;

    /**
     * 分页查询
     */
    public PageResult<ReservationVO> queryPage(ReservationQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ReservationVO> list = reservationDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(ReservationAddForm addForm) {
        EnterpriseVO enterprise = enterpriseService.getDetailByName(addForm.getEnterpriseName());
        if (null == enterprise) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }

        ReservationEntity reservationEntity = SmartBeanUtil.copy(addForm, ReservationEntity.class);
        reservationEntity.setEnterpriseId(enterprise.getEnterpriseId());
        reservationEntity.setReservationState("RESERVED");
        reservationEntity.setReservationCode(generateVerificationCode());
        reservationDao.insert(reservationEntity);
        return ResponseDTO.ok();
    }

    /**
     * 生成6位数字验证码
     */
    private String generateVerificationCode() {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            code.append(random.nextInt(10));
        }
        return code.toString();
    }


    /**
     * 更新
     */
    public ResponseDTO<String> update(ReservationUpdateForm updateForm) {
        ReservationEntity reservationEntity = SmartBeanUtil.copy(updateForm, ReservationEntity.class);
        reservationDao.updateById(reservationEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        reservationDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long reservationId) {
        if (null == reservationId) {
            return ResponseDTO.ok();
        }

        reservationDao.updateDeleted(reservationId, true);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> reservationConfirm(Long reservationId) {
        ReservationEntity reservationEntity = reservationDao.selectById(reservationId);
        if (null == reservationEntity || reservationEntity.getDeletedFlag()) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST);
        }
        if (!ReservationTypeEnum.TOBECONFIRMED.getValue().equals(reservationEntity.getReservationState())) {
            return ResponseDTO.error(UserErrorCode.DATA_NOT_MATCHING);
        }
        reservationDao.updateReservationState(reservationId, ReservationTypeEnum.CONFIRMED.getValue());
        return ResponseDTO.ok();
    }

    public List<ReservationVO> queryPage4public(@Valid ReservationQueryFormPub queryForm) {

        List<ReservationVO> list = reservationDao.queryPage4public(queryForm);

        return list;
    }
}
